package com.ws.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.bean.*;
import com.ws.bean.vo.RouteVo;
import com.ws.service.*;
import com.ws.utils.Res;
import com.ws.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/qianTai")
public class QianTaiController {
    @Autowired
    QianTaiService qianTaiService;
    //轮播
    @Autowired
    CarouselService carouselService;
    //友情链接
    @Autowired
    FriendlinkService friendLinkService;
    //分类
    @Autowired
    CategorieService categorieService;
    //路线
    @Autowired
    RouteService routeService;
    //附图
    @Autowired
    ImageService imageService;
    //收藏
    @Autowired
    FavoriteService favoriteService;
    //用户
    @Autowired
    UserService userService;

    // 跳转首页
    @RequestMapping("toindex")
    public String toindex(Model model){
        // 友情链接
        List<Friendlinks> linkList = friendLinkService.list(null);
        model.addAttribute("linkList",linkList);
        // 轮播图
        List<Carousel> carouselList = carouselService.list();
        model.addAttribute("carouselList",carouselList);
        //分类
        List<Categories> categorieList = categorieService.list();
        model.addAttribute("categorieList", categorieList);
        // 路线
        List<Routes> routesList = routeService.list2(null);
        model.addAttribute("routesList", routesList);
        return "home/index";
    }
    // 跳转搜索页
    @RequestMapping("tosSuoSuo")
    public String tosSuoSuo(String route_name,Model model){
        RouteVo routeVo = new RouteVo();
        routeVo.setRoute_name(route_name);
        List<Routes> list = routeService.list2(routeVo);
        model.addAttribute("list",list);
        model.addAttribute("routeVo",routeVo);
        return "home/route_list";
    }
    //详情页
    @RequestMapping("toxiangqing")
    public String toxiangqing(Integer route_id, Model model ){
        // 路线
        Routes route = routeService.getRouteById(route_id);
        model.addAttribute("route",route);
        //附图
        List<Images> imagesList = imageService.listById(route_id);
        model.addAttribute("imagesList",imagesList);
        return "home/route_detail";
    }
    //收藏
    @RequestMapping("tofavor")
    public String tofavor(){
        return "home/myfavorite";
    }
    //收藏排行榜
    @RequestMapping("tofavorRank")
    public String tofavorRank(){
        return "home/favoriterank";
    }
    //个人主页
    @RequestMapping("tohome")
    public String tohome(Model model, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if (user!=null){
            List<Favorites> list = favoriteService.selMeFavorite(user.getUser_id());
            model.addAttribute("list",list);
        }
        return "home/geRenZhuYe";
    }
    //注册
    @RequestMapping("zhuce")
    public String zhuce(User user, MultipartFile image) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            user.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));
        }
        user.setUser_pass(SecurityUtils.encrypt(user.getUser_pass()));
        qianTaiService.zhuce(user);
        return "../qianTaiLogin";
    }
    //登录
    @RequestMapping("login")
    public String login(String user_name, String user_pass, HttpServletRequest request){
        User u = qianTaiService.login(user_name);
        if (u!=null){
            boolean verify = SecurityUtils.verify(user_pass, u.getUser_pass());
            if(verify){
                request.getSession().setAttribute("user",u);
                return "redirect:/qianTai/toindex";
            }
        }
        return "../qianTaiLogin";
    }
    //退出
    @RequestMapping("tuichu")
    public String tuichu(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:/qianTai/toindex";
    }
    //收藏
    @PostMapping("shouCang")
    @ResponseBody
    public boolean shouCang(Integer route_id, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Favorites favorites = favoriteService.selectUserAndRoute(user.getUser_id(),route_id);
        boolean flag;
        if (favorites == null) {
            Favorites favorite = new Favorites();
            favorite.setUser_id(user.getUser_id());
            favorite.setRoute_id(route_id);
            favoriteService.addFavorite(favorite);//添加收藏
            routeService.updateFavoriteNum(route_id);
            flag = true;
        }else{
            favoriteService.delete(favorites.getFavorite_id());
            routeService.updateFavoriteDeleteNum(route_id);
            flag = false;
        }
        return flag;
    }
    @RequestMapping("deleteFavorite")
    public String deleteFavorite(Integer route_id, HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        Favorites favorites = favoriteService.selectUserAndRoute(user.getUser_id(),route_id);
        if (favorites!=null){
            favoriteService.delete(favorites.getFavorite_id());
            routeService.updateFavoriteDeleteNum(route_id);
        }
        return "redirect:/qianTai/tohome";
    }

    // 更新个人信息
    @RequestMapping("update")
    public String update(User user, MultipartFile image,String tx, HttpServletRequest request) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            user.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));

            User olduser = userService.selUserOne(user.getUser_id());
            String oldTx = olduser.getUser_tx();

            if (oldTx!=null&&!oldTx.equals(user.getUser_tx())){
                File oldFile = new File(Res.fileDir+oldTx);
                if (oldFile.exists()){
                    oldFile.delete();
                }
            }
        }else{
            user.setUser_tx(tx);
        }
        user.setUser_pass(SecurityUtils.encrypt(user.getUser_pass()));
        int update = qianTaiService.update(user);
        request.getSession().setAttribute("user",user);
        return "redirect:/qianTai/tohome";
    }

}
